﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Furion;
using GnpFine.Application.NmpaSystem;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace GnpFine.Application.NmpaSystem
{
    class KdCloudOpertionUtils
    {

        public static ResponseModel<T> GetCusdata<T>(string servicekey, string param)
        {
            var KdConnString= App.GetOptions<NmpaOptionsSet>();


            string baseUrl = KdConnString.KDCLOUD_URL; //服务器地址
            HttpClientEx httpClient = new HttpClientEx();
            httpClient.Url = string.Format("{0}/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
                baseUrl);
            List<object> Parameters = new List<object>
            {
                KdConnString.KDCLOUD_DBID, //帐套Id
                KdConnString.KDCLOUD_USERNAME, //用户名
                KdConnString.KDCLOUD_PASSWORD, //密码
                2052 //多语言：中文
            };
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            string resultstring = httpClient.AsyncRequest();
            var iResult = JObject.Parse(resultstring)["LoginResultType"].Value<int>();
            if (iResult == 1)
            {

                httpClient.Url = string.Concat(baseUrl, servicekey, ".common.kdsvc");


                string model = param;
                List<object> argslList = new List<object>() { model };


                httpClient.Content = JsonConvert.SerializeObject(argslList);
                string response = httpClient.AsyncRequest();

                // Log.Information($"已经获取到金蝶{servicekey}的数据：");
                

                var jSetting = new JsonSerializerSettings();
                jSetting.NullValueHandling = NullValueHandling.Ignore;


                ResponseModel<T> tmp3 = JsonConvert.DeserializeObject<ResponseModel<T>>(response, jSetting);
                return tmp3;

            }
            else
            {
                return new ResponseModel<T>(false, JObject.Parse(resultstring)["Message"].Value<string>());
            }
        }


    }
}